/*
 * ContactDetailTypeListModel.java
 *
 * Created on 27 December 2003, 02:49
 * Copyright (C) 2003, Velocityme Partnership
 */

package com.velocityme.client.gui.contactdetail;

import com.velocityme.client.gui.VectorTableModel;
import com.velocityme.valueobjects.ContactDetailTypeValue;
import com.velocityme.valueobjects.ContactDetailTypeValueToString;
import com.velocityme.valueobjects.NotificationMechanismValue;
import com.velocityme.valueobjects.NotificationMechanismValueToString;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 *
 * @author  Robert Crida Work
 */
public class ContactDetailTypeTableModel extends VectorTableModel {
    
    private NotificationMechanismValue m_defaultNotificationMechanismValue = null;
    private boolean m_editable = false;
    
    /** Creates a new instance of ContactDetailTypeListModel */
    public ContactDetailTypeTableModel() {
        super(new String[] {"Value", "Notification Mechanism", "Show Type", "Show In Directory"});
    }
    
    public Collection getElements() {
        Collection results = new ArrayList();
        for (int row = 0; row < getRowCount(); row++) {
            ContactDetailTypeValue value = (ContactDetailTypeValue)getValueAt(row,4); 
            value.setName((String)getValueAt(row,0));
            value.setNotificationMechanismValue((NotificationMechanismValue)getValueAt(row,1));
            value.setShowType((Boolean)getValueAt(row,2));
            value.setShowInDirectory((Boolean)getValueAt(row,3));
            value.setSequenceNumber(new Integer(row));
            results.add(value);
        }
        return results;
    }
    
    public void addValues(Collection p_values) {
        m_dataRows.clear();
        SortedSet sortedSet = new TreeSet();       
        Iterator i = p_values.iterator();
        while(i.hasNext())
            sortedSet.add(new ContactDetailTypeValueToString((ContactDetailTypeValue)i.next()));
        i = sortedSet.iterator();
        while(i.hasNext()) {
            ContactDetailTypeValue value = (ContactDetailTypeValue)i.next();
            String name = value.getName();
            NotificationMechanismValue mechanism = new NotificationMechanismValueToString(value.getNotificationMechanismValue());
            Boolean showType = value.getShowType();
            Boolean showInDirectory = value.getShowInDirectory();
            m_dataRows.addElement(new Object[]{ name, mechanism, showType, showInDirectory, value });
        }
        fireTableRowsInserted(m_dataRows.size() - 1, m_dataRows.size());
        if (p_values.isEmpty())
            addValueAt(0);
    }
    
    public void addValueAt(int index) {
        m_dataRows.add(index, new Object[]{ new String(), m_defaultNotificationMechanismValue, new Boolean(true), new Boolean(true), new ContactDetailTypeValue() });
        fireTableRowsInserted(index, index);
    }
    
    public void swapValues(int index1, int index2) {
        Object temp = m_dataRows.get(index1);
        m_dataRows.set(index1, m_dataRows.get(index2));
        m_dataRows.set(index2, temp);
        fireTableRowsUpdated(index1, index2);
    }
    
    public void setEditable(boolean editable, NotificationMechanismValue p_defaultNotificationMechanismValue) {
        m_editable = editable;
        m_defaultNotificationMechanismValue = p_defaultNotificationMechanismValue;
    }
    
    public boolean isCellEditable(int row, int col) {
        return m_editable;
    }
    
    public void setValueAt(Object value, int row, int col) {
        ((Object[])m_dataRows.get(row))[col] = value;
        fireTableDataChanged();
    }
    
}
